package net.towee.client.com.repo.view;

import java.util.ArrayList;
import java.util.List;

import net.towee.client.com.repo.presenter.RepositoryNodeSelectorPresenter.Display;
import net.towee.client.i18n.ToweeConstants;
import net.towee.client.resources.ClientResource;
import net.towee.client.widgets.SpanPanel;
import net.towee.model.repo.NodeData;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.inject.Inject;

public class RepositoryNodeSelectorView extends FlowPanel implements Display {
	
	private class NodeLabel extends Label implements HasSelectionHandlers<NodeData> {

		public NodeLabel(String name) {
			super(name);
		}

		@Override
		public HandlerRegistration addSelectionHandler(SelectionHandler<NodeData> handler) {
			return addHandler(handler, SelectionEvent.getType());
		}
	}
	
	private final ToweeConstants constants;
	private final ClientResource resource;
	
	@Inject
	public RepositoryNodeSelectorView(ToweeConstants constants, ClientResource resource){
		this.constants = constants;
		this.resource = resource;
		
		setStyleName( resource.css().repoNodeSelector() );
		getElement().setAttribute("path", "");
	}
	
	@Override
	public HasClickHandlers getStartButton() {
		return new Button();
	}

	@Override
	public <T extends HasClickHandlers, HasSelectionHandlers> List<T> showPath(List<NodeData> nodes) {
		// Remove old path
		clear();
		
		// Create array for new path
		List<T> path = new ArrayList<T>();
		
		for(NodeData node : nodes) {
			// Add label
			NodeLabel l = new NodeLabel( node.getName() );
			l.setLayoutData( node );
			path.add( (T)l );
			add(l);
			
			// Add arrow
			add(new SpanPanel("→"));
		}
		
		// Remove last arrow
		getWidget( getWidgetCount()-1 ).removeFromParent();
		return path;
	}

	@Override
	public <T extends HasClickHandlers, HasSelectionHandlers> List<T> showSubMenu(NodeData parentNode,
			List<NodeData> nodes) {
		return new ArrayList<T>();
	}
}
